package com.nf.dao.impl;

import com.nf.dao.ShowDateVoDao;
import com.nf.vo.ShowDateVo;
import com.nf.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ShowDateVoDaoImpl implements ShowDateVoDao {
    @Override
    public List<ShowDateVo> selectByFilmId(int filmId) {
        List<ShowDateVo> list=new ArrayList<ShowDateVo>();

        Connection conn= DBUtil.getConnection();
        String sql="select distinct show_Date from showInfo where film_id=?";
        PreparedStatement pstmt=null;
        ResultSet rst=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,filmId);

            rst=pstmt.executeQuery();
            while (rst.next()){
                String showDate=rst.getString(1);

                // 获取当前日期
                Date date = new Date();
                DateFormat matter = new SimpleDateFormat("yyyy-MM-dd");

                // 判断放映日期是否大于当前日期，如若大于当前日期则小于0，小于则大于0，等于则等于0
                if((matter.format(date)).compareTo(showDate)>0){
                    ShowDateVo show=new ShowDateVo(showDate);
                    list.add(show);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(rst,pstmt,conn);
        }

        return list;
    }
}
